package com.gopay.common.cps.dao.wallet.promotions.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.promotions.WaPromotionsLogDao;
import com.gopay.common.domain.dps.WaPromotionsLog;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.StringTools;
/**
 * 
 * @author dps
 *
 */
@Repository("waPromotionsLogDAO")
public class WaPromotionsLogDaoImpl extends BaseDAOImpl<WaPromotionsLog, Integer> implements WaPromotionsLogDao {

	protected Logger logger = LoggerFactory.getLogger(WaPromotionsLogDaoImpl.class);
	@Override
	public WaPromotionsLog getWaPromotionsLogByType(String type) throws GopayException {
		Map<String, Object> param = new HashMap<String, Object>();
		String hql = "from WaPromotionsLog t where t.type=:type order by t.createTime desc";
		param.put("type", type);
		logger.error("WaPromotionsLog query HQL: "+hql);
		WaPromotionsLog waPromotionsLog = new WaPromotionsLog();
		List<WaPromotionsLog> promotionsLogs = this.findByHql(hql, param);
		if( promotionsLogs != null && promotionsLogs.size() > 0 ){
			waPromotionsLog = promotionsLogs.get(0);
		}
		return waPromotionsLog;
	}

	@Override
	public WaPromotionsLog getWaPromotionsLogByCustId(String custId) {
		Map<String, Object> param = new HashMap<String, Object>();
		String hql = "from WaPromotionsLog t where t.type=:type AND t.custId=:custId";
		param.put("type", "1");
		param.put("custId", custId);
		WaPromotionsLog waPromotionsLog = new WaPromotionsLog();
		List<WaPromotionsLog> promotionsLogs = this.findByHql(hql, param);
		if( promotionsLogs != null && promotionsLogs.size() > 0 ){
			waPromotionsLog = promotionsLogs.get(0);
		}
		return waPromotionsLog;
	}

	@Override
	public List<WaPromotionsLog> getByTypeAndReward(String type,BigDecimal reward) throws GopayException {
		Map<String, Object> param = new HashMap<String, Object>();
		String hql = "from WaPromotionsLog t where t.type=:type and t.reward=:reward";
		param.put("type", type);
		param.put("reward", reward);
		List<WaPromotionsLog> promotionsLogs = this.findByHql(hql, param);
		return promotionsLogs;
	}
	/**
	 * 汇总type=4、5、6时 ，已发红包的总金额
	 * @return 已发红包的总金额
	 */
	@Override
	public String findSumAmtByType() throws GopayException {
		Map<String, Object> param = new HashMap<String, Object>();
		String sql = "select sum(t.reward) from Wa_Promotions_Log t where t.type=:type1 or t.type=:type2 or t.type=:type3 ";
		param.put("type1", "4");
		param.put("type2", "5");
		param.put("type3", "6");
		List expendInfoList =  this.findBySql(sql,param);
		String result = StringTools.checkNulltoString(expendInfoList.get(0));
		if("".equals(result)){
			return "0.00";
		}
		return result;
	}
	
	
}
